/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web.front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.JavaType;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.JsonUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.service.FileTplService;
import com.thinkgem.jeesite.modules.cms.service.SiteService;
import com.thinkgem.jeesite.modules.cms.utils.CmsUtils;
import com.thinkgem.jeesite.modules.shop.entity.Goods;
import com.thinkgem.jeesite.modules.shop.entity.SpecificationItem;
import com.thinkgem.jeesite.modules.shop.service.AttributeService;
import com.thinkgem.jeesite.modules.shop.service.BrandService;
import com.thinkgem.jeesite.modules.shop.service.GoodsCategoryService;
import com.thinkgem.jeesite.modules.shop.service.GoodsService;
import com.thinkgem.jeesite.modules.shop.service.ParameterService;
import com.thinkgem.jeesite.modules.shop.service.ParameterValueService;
import com.thinkgem.jeesite.modules.shop.service.ProductService;
import com.thinkgem.jeesite.modules.shop.service.ShopTagService;
import com.thinkgem.jeesite.modules.shop.service.SpecificationService;

/**
 * 商品管理Controller
 * @author xxs
 * @version 2016-03-21
 */
@Controller("shopGoodsController")
@RequestMapping(value = "${frontPath}/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ProductService productService;
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	@Autowired
	private FileTplService fileTplService;
	@Autowired
   	private BrandService brandService;
	@Autowired
	private SpecificationService specificationService;
	@Autowired
	private AttributeService attributeService;
	@Autowired
   	private ParameterService parameterService ;
	@Autowired
	private SiteService siteService;
	@Autowired
   	private ParameterValueService parameterValueService;
	@Autowired
	private ShopTagService shopTagService;
	
	@RequestMapping(value = "{goodsId}${urlSuffix}")
	public String detail(@PathVariable String goodsId, Model model) {
		Site site = CmsUtils.getSite(Site.defaultSiteId());
		model.addAttribute("site", site);
		Goods goods = goodsService.get(goodsId);
		System.out.println("sis:"+goods.getSis());
		JavaType javaType = JsonUtils.getCollectionType(ArrayList.class, SpecificationItem.class); 
		System.out.println("-0000000:"+JsonUtils.toObject(goods.getSis(), javaType).toString());
		List<SpecificationItem> specificationItems = JsonUtils.toObject(goods.getSis(), javaType);
		model.addAttribute("specifications", specificationService.getSpecificationByCategoryId(goods.getGoodsCategory().getId()));
		model.addAttribute("goods", goods);
		model.addAttribute("specificationItems", specificationItems);
		System.out.println("modules/shop/front/themes/"+CmsUtils.getSite(Site.defaultSiteId()).getTheme()+"/detail");
		return "modules/shop/front/themes/"+site.getTheme()+"/"+goods.getCustomContentView();
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Site site = CmsUtils.getSite(Site.defaultSiteId());
		Page<Goods> page = goodsService.findPage(new Page<Goods>(request, response), goods); 
		model.addAttribute("page", page);
		return "modules/shop/front/themes/"+site.getTheme()+"/detail";
	}
}